package com.allianz.webapps.controllers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.webapps.https.interfaces.appInitPurchaseNote.InitPurchaseNote;
import com.allianz.webapps.orderspojo.AppBase;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping(value="main")
public class AppInitPurchaseNoteController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private InitPurchaseNote initPurchaseNote;
	@Autowired
	private ObjectMapper objectMapper;
	
	/**
	 * 获取产品信息
	 */
	@RequestMapping(value="initPurchaseNote")
	@ResponseBody
	public AppBase initPurchaseNote(@RequestParam(value="jsonParam") String jsonParam,HttpServletRequest request,HttpServletResponse response)
			throws Exception{
		AppBase appBase=objectMapper.readValue(jsonParam, AppBase.class);
		String body=initPurchaseNote.requestLoginParam(appBase);
		logger.info(body);
		JSONObject json=new JSONObject(body);
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		String body1 = null;
		if (json.get("Body").equals(null)) {
			if(!("S").equals(json.getString("Status")))
				throw new Exception(json.getString("ErrMsg"));
			else
				return null;
		}else{
			body1 = json.getString("Body");
			appBase = objectMapper.readValue(body1, AppBase.class);
		}
		return appBase;
	}
}
